package com.guoyh.xprodatas.ktx

import com.guoyh.baseutils.aroute.AppPath
import com.guoyh.baseutils.aroute.LoginPath
import com.guoyh.bilnet.exception.TokenException
import com.guoyh.pronet.service.BaseService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import rxhttp.RxHttpPlugins
import rxhttp.wrapper.param.RxHttpFormParam
import java.util.*

/**
 * @CreateDate: 7/15/21 10:39 AM
 * @Author: guoyh
 * @Description: java类作用描述
 */
/**
 * 封装单个的请求  异步
 */
fun <T> CoroutineScope.requestAsync(block: suspend CoroutineScope.() -> T): Deferred<T>? {
    try {
        return async {
            val a = block()
            a
        }
    } catch (e: TokenException) {
        if (e.mCode == 904) {
            BaseService.refreshToken()
            requestAsync(block)
        } else {
            RxHttpPlugins.cancelAll()
            LoginPath.startLoginAct()
        }
        return null
    }
}


fun RxHttpFormParam.addParams(vararg params:Pair<String,Any>): RxHttpFormParam {
//    com.xuexiang.xui.utils.CollectionUtils.
    val paramsMap = sortedMapOf<String, Any>()
    params.forEach {
        paramsMap.put(it.first,it.second)
    }
    addAll(paramsMap)
    return this
}